package com.hw.paltfrom.utils;

import java.lang.reflect.Field;

import org.apache.commons.lang3.StringUtils;

import com.hw.paltfrom.anno.Verify;



/**
 * VO参数校验辅助类
 *
 */
public class VerifyUtil {
	
	public  static <T> boolean verifyParams(T obj,Class<T> clazz) {
		Field[] fields = clazz.getDeclaredFields();
		try {
			for (Field field : fields) {
				// 暴力反射,获取私有属性值
				field.setAccessible(true);
				// 获取属性的注解
				Verify verify = field.getAnnotation(Verify.class);
				if (verify != null) {
					
					
					if(verify.require()) {
						if(null==obj) {
							return false;
						}
						if(StringUtils.isBlank(String.valueOf(field.get(obj)))) {
							return false;
						}
						
					}
					
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
		
		
		
	}
	
	public static Boolean verifyParams(Object obj) {
		Boolean flag = true;
		Field[] fields = obj.getClass().getDeclaredFields();
		try {
			for (Field field : fields) {
				// 暴力反射,获取私有属性值
				field.setAccessible(true);
				// 获取属性的注解
				Verify verify = field.getAnnotation(Verify.class);
				if (verify != null) {
					// 获取属性的值
					Object objValue = field.get(obj);
					if (verify.require() && objValue == null) {
						flag = false;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}

}
